﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Text;

namespace xuptlibsearch
{
    public partial class modify : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Charset = "UTF-8";
            Response.ContentType = "text/json";
            string ID = Request.Form["ID"];
            string CurrentPassword = Request.Form["Password"];
            string NewPassword = Request.Form["NewPassword"];
            string Verify = Request.Form["Verify"];
            //string ID = Request.QueryString["ID"];
            //string CurrentPassword = Request.QueryString["Password"];
            //string NewPassword = Request.QueryString["NewPassword"];
            //string Verify = Request.QueryString["Verify"];
            if(
                (ID==null || ID=="") ||
                (CurrentPassword==null || CurrentPassword=="") ||
                (NewPassword==null || NewPassword=="") ||
                (Verify==null || Verify=="")
                )
            {
                Response.Write("Param_Error");
                return;
            }
            if (NewPassword != Verify)
            {
                Response.Write("NewPassword_Inconformity");
                return;
            }
            ReqServer(ID, CurrentPassword, NewPassword);
        }
        void ReqServer(string id,string pass,string new_pass)
        {
            string restr = "";
            try
            {
                CookieContainer cookie = new CookieContainer();
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(string.Format("http://222.24.3.7:8080/reader/login.jsp?str_kind=login&barcode={0}&fangshi=0&password={1}&x=0&y=0", id, pass));
                req.CookieContainer = cookie;
                req.GetResponse();
                req = (HttpWebRequest)WebRequest.Create("http://222.24.3.7:8080/reader/infoChange.jsp?kind=submit");
                req.CookieContainer = cookie;
                req.Method = "POST";
                Stream reqStream = req.GetRequestStream();
                byte[] PostBin = Encoding.GetEncoding("GB2312").GetBytes(string.Format("oldpass={0}&newpass1={1}&newpass2={1}", pass, new_pass));
                reqStream.Write(PostBin, 0, PostBin.Length);
                WebResponse rp = req.GetResponse();
                Stream str = rp.GetResponseStream();
                StreamReader reader = new StreamReader(str, Encoding.GetEncoding("gb2312"));
                restr = reader.ReadToEnd();
            }
            catch
            {
                Response.Write("Server_Error");
                return;
            }
            if (restr.Contains("读者信息已经成功修改"))
            {
                Response.Write("Modify_Successed");
                return;
            }
            else
            {
                Response.Write("Modify_Failed");
                return;
            }
        }
    }
}